﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel.Syndication;
using System.Xml;

public partial class GenerateFeed : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string feedType = Request.QueryString["outputType"];
            SyndicationFeed feed = null;
            feed = GetSynicationFeed();


            if (feedType == "rss")
            {
                FeedHelper.DisplaySyndicationFeedAsRss(feed, this);
            }
            else if (feedType == "atom")
            {
                FeedHelper.DisplaySyndicationFeedAsAtom(feed, this);
            }
            else
            {
                FeedHelper.DisplaySyndicationFeedAsRss(feed, this);
            }

        }
    }

    private SyndicationFeed GetSynicationFeed()
    {
        SyndicationFeed feed = new SyndicationFeed("Sport Portal", "News, Images and Blogs  on " + Request.Url.Authority, new Uri("http://" + Request.Url.Authority));
        feed.Authors.Add(new SyndicationPerson
            ("webmaster@" + Request.Url.Authority,
            "Web Master",
            Request.ApplicationPath));

        feed.Generator = Request.Url.Authority + " RSS Generator";
        feed.Language = "en-US";
        List<SyndicationItem> feedItems = new List<SyndicationItem>();
        List<string> trends = SportHelper.GetSports();
        for (int i = 0; i < trends.Count; i++)
        {
            string trendItem = trends[i];

            SyndicationItem item = new SyndicationItem();
            item.Id = trendItem;
            item.Title = new TextSyndicationContent(trendItem);
            item.PublishDate = new DateTimeOffset(DateTime.Now);
            item.Content = new TextSyndicationContent(trendItem, TextSyndicationContentKind.Html);
            string permalink;
            permalink = "http://" + Request.Url.Authority;
            if (Request.ApplicationPath.Length > 1)
            {
                permalink += Request.ApplicationPath;
            }
            permalink += "/page.aspx?sport=" + trendItem;
            item.AddPermalink(new Uri(permalink));
            feedItems.Add(item);
        }
        feed.Items = feedItems;
        return feed;
    }
}

public class FeedHelper
{
    public static void DisplaySyndicationFeedAsRss(SyndicationFeed feed, Page page)
    {
        page.Response.Buffer = false;
        page.Response.Clear();
        page.Response.ContentType = "application/xml";

        using (XmlWriter writer = XmlWriter.Create(page.Response.OutputStream))
        {
            Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);
            rssFormatter.WriteTo(writer);
            writer.Flush();
        }
        page.Response.End();

    }

    public static void DisplaySyndicationFeedAsAtom(SyndicationFeed feed, Page page)
    {
        page.Response.Buffer = false;
        page.Response.Clear();
        page.Response.ContentType = "application/xml";

        using (XmlWriter writer = XmlWriter.Create(page.Response.OutputStream))
        {
            Atom10FeedFormatter feedFormatter = new Atom10FeedFormatter(feed);
            feedFormatter.WriteTo(writer);
            writer.Flush();
        }
        page.Response.End();

    }
}
